home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / util / moni / Scout-src.lha / source / objects / scout_assigns.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-09-17  |  15.1 KB  |  428 lines

  1. /**
  2.  * Scout - The Amiga System Monitor
  3.  *
  4.  *------------------------------------------------------------------
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * You must not use this source code to gain profit of any kind!
  21.  *
  22.  *------------------------------------------------------------------
  23.  *
  24.  * @author Andreas Gelhausen
  25.  * @author Richard Körber <rkoerber@gmx.de>
  26.  */
  27.  
  28. #include "system_headers.h"
  29.  
  30. struct AssignsCallbackUserData {
  31.     APTR ud_Tree;
  32.     ULONG ud_Count;
  33. };
  34.  
  35. static __asm __saveds LONG asstree_confunc(register __a2 Object *obj, register __a1 struct MUIP_NListtree_ConstructMessage *msg, register __a0 struct Hook *hook)
  36. {
  37.     return AllocListEntry(msg->MemPool, msg->UserData, sizeof(struct AssignEntry));
  38. }
  39.  
  40. MakeHook(asstree_conhook, asstree_confunc);
  41.  
  42. static __asm __saveds LONG asstree_desfunc(register __a2 Object *obj, register __a1 struct MUIP_NListtree_ConstructMessage *msg, register __a0 struct Hook *hook)
  43. {
  44.     FreeListEntry(msg->MemPool, &msg->UserData);
  45.  
  46.     return 0;
  47. }
  48.  
  49. MakeHook(asstree_deshook, asstree_desfunc);
  50.  
  51. static __asm __saveds LONG asstree_dspfunc(register __a2 Object *obj, register __a1 struct MUIP_NListtree_DisplayMessage *msg, register __a0 struct Hook *hook)
  52. {
  53.     static UBYTE *empty = "\0";
  54.  
  55.     if (msg->TreeNode != NULL) {
  56.         struct AssignEntry *ae = msg->TreeNode->tn_User;
  57.  
  58.         if (stricmp(ae->ae_Type, "ADD_NODE") == 0) {
  59.             msg->Array[0] = ae->ae_Address;
  60.             msg->Array[1] = empty;
  61.             msg->Array[2] = ae->ae_Path;
  62.         } else {
  63.             msg->Array[0] = ae->ae_Address;
  64.             msg->Array[1] = ae->ae_Name;
  65.             msg->Array[2] = ae->ae_Path;
  66.         }
  67.     } else {
  68.         msg->Array[0] = "Address";
  69.         msg->Array[1] = "Name";
  70.         msg->Array[2] = "Path";
  71.         msg->Preparse[0] = MUIX_B;
  72.         msg->Preparse[1] = MUIX_B;
  73.         msg->Preparse[2] = MUIX_B;
  74.     }
  75.  
  76.     return 0;
  77. }
  78.  
  79. MakeHook(asstree_dsphook, asstree_dspfunc);
  80.  
  81. static __asm __saveds LONG asstree_cmpfunc(register __a2 Object *obj, register __a1 struct MUIP_NListtree_CompareMessage *msg, register __a0 struct Hook *hook)
  82. {
  83.     LONG cmp;
  84.     struct AssignEntry *ae1, *ae2;
  85.  
  86.     ae1 = (struct AssignEntry *)msg->TreeNode1->tn_User;
  87.     ae2 = (struct AssignEntry *)msg->TreeNode2->tn_User;
  88.  
  89.     cmp = stricmp(ae1->ae_Name, ae2->ae_Name);
  90.     if (cmp == 0) cmp = stricmp(ae1->ae_RealPath, ae2->ae_RealPath);
  91.  
  92.     return cmp;
  93. }
  94.  
  95. MakeHook(asstree_cmphook, asstree_cmpfunc);
  96.  
  97. static __asm __saveds LONG asstree_findfunc(register __a2 Object *obj, register __a1 struct MUIP_NListtree_FindUserDataMessage *msg, register __a0 struct Hook *hook)
  98. {
  99.     struct AssignEntry *ae;
  100.  
  101.     ae = (struct AssignEntry *)msg->UserData;
  102.  
  103.     if (ae) {
  104.         return stricmp((UBYTE *)msg->User, ae->ae_Name);
  105.     } else {
  106.         return ~0;
  107.     }
  108. }
  109.  
  110. MakeHook(asstree_findhook, asstree_findfunc);
  111.  
  112. static void ReceiveList( void (* callback)( struct AssignEntry *ae, void *userData ),
  113.                          void *userData )
  114. {
  115.     struct AssignEntry *ae;
  116.  
  117.     if (ae = tbAllocVecPooled(globalPool, sizeof(struct AssignEntry))) {
  118.         if (SendDaemon("GetAssList")) {
  119.             while (ReceiveDecodedEntry((UBYTE *)ae, sizeof(struct AssignEntry))) {
  120.                 callback(ae, userData);
  121.             }
  122.         }
  123.  
  124.         tbFreeVecPooled(globalPool, ae);
  125.     }
  126. }
  127.  
  128. static void IterateList( void (* callback)( struct AssignEntry *ae, void *userData ),
  129.                          void *userData )
  130. {
  131.     struct FileInfoBlock *fib;
  132.  
  133.     if (fib = AllocDosObject(DOS_FIB, TAG_DONE)) {
  134.         UBYTE *tmp;
  135.         struct AssignEntry *ae;
  136.  
  137.         tmp = tbAllocVecPooled(globalPool, PATH_LENGTH);
  138.         ae = tbAllocVecPooled(globalPool, sizeof(struct AssignEntry));
  139.  
  140.         if (tmp != NULL && ae != NULL) {
  141.             struct DosList *dol;
  142.  
  143.             dol = LockDosList(LDF_READ | LDF_ASSIGNS);
  144.  
  145.             while (dol = NextDosEntry(dol, LDF_ASSIGNS)) {
  146.                 struct AssignList *list;
  147.  
  148.                 ae->ae_Addr = dol;
  149.                 _snprintf(ae->ae_Address, sizeof(ae->ae_Address), "$%08lx", dol);
  150.                 b2cstrn(dol->dol_Name, ae->ae_Name, sizeof(ae->ae_Name));
  151.  
  152.                 if (dol->dol_Lock != (BPTR)NULL && Examine(dol->dol_Lock, fib)) {
  153.                     NameFromLock(dol->dol_Lock, tmp, PATH_LENGTH);
  154.  
  155.                     stccpy(ae->ae_Path, tmp, sizeof(ae->ae_Path));
  156.                     stccpy(ae->ae_RealPath, tmp, sizeof(ae->ae_RealPath));
  157.                     stccpy(ae->ae_Type, "NORMAL", sizeof(ae->ae_Type));
  158.                 } else {
  159.                     switch (dol->dol_Type) {
  160.                         case DLT_LATE:
  161.                             _snprintf(ae->ae_Path, sizeof(ae->ae_Path), "<%s>", dol->dol_misc.dol_assign.dol_AssignName);
  162.                             stccpy(ae->ae_RealPath, dol->dol_misc.dol_assign.dol_AssignName, sizeof(ae->ae_RealPath));
  163.                             stccpy(ae->ae_Type, "LATE", sizeof(ae->ae_Type));
  164.                             break;
  165.  
  166.                         case DLT_NONBINDING:
  167.                             _snprintf(ae->ae_Path, sizeof(ae->ae_Path), "[%s]", dol->dol_misc.dol_assign.dol_AssignName);
  168.                             stccpy(ae->ae_RealPath, dol->dol_misc.dol_assign.dol_AssignName, sizeof(ae->ae_RealPath));
  169.                             stccpy(ae->ae_Type, "PATH", sizeof(ae->ae_Type));
  170.                             break;
  171.  
  172.                         default:
  173.                             ae->ae_Path[0] = 0x00;
  174.                             ae->ae_RealPath[0] = 0x00;
  175.                             stccpy(ae->ae_Type, "NORMAL", sizeof(ae->ae_Type));
  176.                             break;
  177.                     }
  178.                 }
  179.  
  180.  
  181.                 if (list = dol->dol_misc.dol_assign.dol_List) {
  182.                     stccpy(ae->ae_Type, "ADD_LIST", sizeof(ae->ae_Type));
  183.                     callback(ae, userData);
  184.  
  185.                     do {
  186.                         ae->ae_Addr = (APTR)list->al_Lock;
  187.                         _snprintf(ae->ae_Address, sizeof(ae->ae_Address), "$%08lx", BADDR(list->al_Lock));
  188.                         b2cstrn(dol->dol_Name, ae->ae_Name, sizeof(ae->ae_Name));
  189.  
  190.                         if (list->al_Lock != (BPTR)NULL && Examine(list->al_Lock, fib)) {
  191.                             NameFromLock(list->al_Lock, tmp, PATH_LENGTH);
  192.  
  193.                             if (dol->dol_Type == DLT_DIRECTORY) {
  194.                                 stccpy(ae->ae_Path, tmp, sizeof(ae->ae_RealPath));
  195.                                 stccpy(ae->ae_RealPath, tmp, sizeof(ae->ae_RealPath));
  196.                             }
  197.                         } else {
  198.                             ae->ae_Path[0] = 0x00;
  199.                             ae->ae_RealPath[0] = 0x00;
  200.                         }
  201.  
  202.                         stccpy(ae->ae_Type, "ADD_NODE", sizeof(ae->ae_Type));
  203.  
  204.                         callback(ae, userData);
  205.  
  206.                         list = (struct AssignList *)list->al_Next;
  207.                     } while (list);
  208.                 } else {
  209.                     callback(ae, userData);
  210.                 }
  211.             }
  212.  
  213.             UnLockDosList(LDF_READ | LDF_ASSIGNS);
  214.         }
  215.  
  216.         if (tmp) tbFreeVecPooled(globalPool, tmp);
  217.         if (ae) tbFreeVecPooled(globalPool, ae);
  218.  
  219.         FreeDosObject(DOS_FIB, fib);
  220.     }
  221. }
  222.  
  223. static void UpdateCallback( struct AssignEntry *ae,
  224.                             void *userData )
  225. {
  226.     struct AssignsCallbackUserData *ud = (struct AssignsCallbackUserData *)userData;
  227.     struct MUI_NListtree_TreeNode *parent;
  228.     ULONG flags;
  229.  
  230.     flags = 0;
  231.     if (stricmp(ae->ae_Type, "ADD_LIST") == 0) flags = TNF_LIST | TNF_OPEN;
  232.  
  233.     parent = (struct MUI_NListtree_TreeNode *)DoMethod(ud->ud_Tree, MUIM_NListtree_FindUserData, MUIV_NListtree_FindUserData_ListNode_Root, ae->ae_Name, MUIV_NListtree_FindUserData_Flag_StartNode);
  234.     DoMethod(ud->ud_Tree, MUIM_NListtree_Insert, ae->ae_Name, ae, parent, MUIV_NListtree_Insert_PrevNode_Sorted, flags);
  235.     ud->ud_Count++;
  236. }
  237.  
  238. static void PrintCallback( struct AssignEntry *ae,
  239.                            void *userData )
  240. {
  241.     PrintFOneLine((BPTR)userData, " %9s %-16s %s\n", ae->ae_Address, ae->ae_Name, ae->ae_Path);
  242. }
  243.  
  244. static void SendCallback( struct AssignEntry *ae,
  245.                           void *userData )
  246. {
  247.     SendEncodedEntry((UBYTE *)ae, sizeof(struct AssignEntry));
  248. }
  249.  
  250. static ULONG __saveds mNew( struct IClass *cl,
  251.                             Object *obj,
  252.                             struct opSet *msg )
  253. {
  254.     APTR asslist, asstree, asstext, asscount, updateButton, printButton, removeButton, exitButton;
  255.  
  256.     if (obj = (Object *)DoSuperNew(cl, obj,
  257.         MUIA_HelpNode, AssignsText,
  258.         MUIA_Window_ID, MakeID('A','S','S','I'),
  259.         WindowContents, VGroup,
  260.  
  261.             Child, asslist = MyNListtreeObject(&asstree, "BAR,BAR,BAR", &asstree_conhook, &asstree_deshook, &asstree_dsphook, &asstree_cmphook, &asstree_findhook, 2),
  262.             Child, MyBelowListview(&asstext, &asscount),
  263.  
  264.             Child, MyVSpace(4),
  265.  
  266.             Child, HGroup, MUIA_Group_SameSize, TRUE,
  267.                 Child, updateButton = MakeButton(txtUpdate),
  268.                 Child, printButton  = MakeButton(txtPrint),
  269.                 Child, removeButton = MakeButton(txtRemove),
  270.                 Child, exitButton   = MakeButton(txtExit),
  271.             End,
  272.         End,
  273.         TAG_MORE, msg->ops_AttrList))
  274.     {
  275.         struct AssignsWinData *awd = INST_DATA(cl, obj);
  276.         APTR parent;
  277.  
  278.         awd->awd_AssignTree = asstree;
  279.         awd->awd_AssignText = asstext;
  280.         awd->awd_AssignCount = asscount;
  281.         awd->awd_RemoveButton = removeButton;
  282.  
  283.         parent = (APTR)GetTagData(MUIA_Window_ParentWindow, (ULONG)NULL, msg->ops_AttrList);
  284.  
  285.         set(obj, MUIA_Window_Title, MyGetWindowTitle("ASSIGNS", awd->awd_Title, sizeof(awd->awd_Title)));
  286.         set(obj, MUIA_Window_ActiveObject, asslist);
  287.         set(removeButton, MUIA_Disabled, TRUE);
  288.  
  289.         DoMethod(parent,       MUIM_Window_AddChildWindow, obj);
  290.         DoMethod(obj,          MUIM_Notify, MUIA_Window_CloseRequest, TRUE,           MUIV_Notify_Application, 5, MUIM_Application_PushMethod, parent, 2, MUIM_Window_RemChildWindow, obj);
  291.         DoMethod(asstree,      MUIM_Notify, MUIA_NListtree_Active,    MUIV_EveryTime, obj,                     1, MUIM_AssignsWin_ListChange);
  292.         DoMethod(updateButton, MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_AssignsWin_Update);
  293.         DoMethod(printButton,  MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_AssignsWin_Print);
  294.         DoMethod(removeButton, MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_AssignsWin_Remove);
  295.         DoMethod(exitButton,   MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     3, MUIM_Set, MUIA_Window_CloseRequest, TRUE);
  296.     }
  297.  
  298.     return (ULONG)obj;
  299. }
  300.  
  301. static ULONG __saveds mDispose( struct IClass *cl,
  302.                                 Object *obj,
  303.                                 struct opSet *msg )
  304. {
  305.     struct AssignsWinData *awd = INST_DATA(cl, obj);
  306.  
  307.     set(obj, MUIA_Window_Open, FALSE);
  308.     DoMethod(awd->awd_AssignTree, MUIM_NListtree_Clear, NULL, 0);
  309.  
  310.     return (DoSuperMethodA(cl, obj, msg));
  311. }
  312.  
  313. static ULONG __saveds mUpdate( struct IClass *cl,
  314.                                Object *obj,
  315.                                Msg msg )
  316. {
  317.     struct AssignsWinData *awd = INST_DATA(cl, obj);
  318.     struct AssignsCallbackUserData ud;
  319.  
  320.     ApplicationSleep(TRUE);
  321.     set(awd->awd_AssignTree, MUIA_NListtree_Quiet, TRUE);
  322.     DoMethod(awd->awd_AssignTree, MUIM_NListtree_Clear, NULL, 0);
  323.  
  324.     ud.ud_Tree = awd->awd_AssignTree;
  325.     ud.ud_Count = 0;
  326.  
  327.     if (clientstate) {
  328.         ReceiveList(UpdateCallback, &ud);
  329.     } else {
  330.         IterateList(UpdateCallback, &ud);
  331.     }
  332.  
  333.     SetCountText(awd->awd_AssignCount, ud.ud_Count);
  334.     MySetContents(awd->awd_AssignText, "");
  335.  
  336.     set(awd->awd_AssignTree, MUIA_NListtree_Quiet, FALSE);
  337.     set(awd->awd_RemoveButton, MUIA_Disabled, TRUE);
  338.     ApplicationSleep(FALSE);
  339.  
  340.     return 0;
  341. }
  342.  
  343. static ULONG __saveds mPrint( struct IClass *cl,
  344.                               Object *obj,
  345.                               Msg msg )
  346. {
  347.     PrintAssigns(NULL);
  348.  
  349.     return 0;
  350. }
  351.  
  352. static ULONG __saveds mRemove( struct IClass *cl,
  353.                                Object *obj,
  354.                                Msg msg )
  355. {
  356.     struct AssignsWinData *awd = INST_DATA(cl, obj);
  357.     struct MUI_NListtree_TreeNode *tn;
  358.  
  359.     if (tn = GetActiveTreeNode(awd->awd_AssignTree)) {
  360.         struct AssignEntry *ae = (struct AssignEntry *)tn->tn_User;
  361.  
  362.         if (stricmp(ae->ae_Type, "ASSIGN_ADD") == 0) {
  363.             if (MyRequest(msgYesNo, msgWantToRemoveAssignFromList, ae->ae_Path, ae->ae_Name)) {
  364.                 MyDoCommand("RemoveAssignList %s $%08lx", ae->ae_Name, ae->ae_Addr);
  365.                 DoMethod(obj, MUIM_AssignsWin_Update);
  366.             }
  367.         } else {
  368.             if (MyRequest(msgYesNo, msgWantToRemoveAssign, ae->ae_Name)) {
  369.                 MyDoCommand("RemoveAssign %s", ae->ae_Name);
  370.                 DoMethod(obj, MUIM_AssignsWin_Update);
  371.             }
  372.         }
  373.     }
  374.  
  375.     return 0;
  376. }
  377.  
  378. static ULONG __saveds mListChange( struct IClass *cl,
  379.                                    Object *obj,
  380.                                    Msg msg )
  381. {
  382.     struct AssignsWinData *awd = INST_DATA(cl, obj);
  383.     struct MUI_NListtree_TreeNode *tn;
  384.  
  385.     if (tn = GetActiveTreeNode(awd->awd_AssignTree)) {
  386.         struct AssignEntry *ae = (struct AssignEntry *)tn->tn_User;
  387.  
  388.         MySetContents(awd->awd_AssignText, "%s \"%s\"", ae->ae_Address, ae->ae_Name);
  389.         set(awd->awd_RemoveButton, MUIA_Disabled, FALSE);
  390.     }
  391.  
  392.     return 0;
  393. }
  394.  
  395. ULONG __asm __saveds AssignsWinDispatcher( register __a0 struct IClass *cl,
  396.                                            register __a2 Object *obj,
  397.                                            register __a1 Msg msg )
  398. {
  399.     switch (msg->MethodID) {
  400.         case OM_NEW:                     return (mNew(cl, obj, (APTR)msg));
  401.         case OM_DISPOSE:                 return (mDispose(cl, obj, (APTR)msg));
  402.         case MUIM_AssignsWin_Update:     return (mUpdate(cl, obj, (APTR)msg));
  403.         case MUIM_AssignsWin_Print:      return (mPrint(cl, obj, (APTR)msg));
  404.         case MUIM_AssignsWin_Remove:     return (mRemove(cl, obj, (APTR)msg));
  405.         case MUIM_AssignsWin_ListChange: return (mListChange(cl, obj, (APTR)msg));
  406.     }
  407.  
  408.     return (DoSuperMethodA(cl, obj, msg));
  409. }
  410.  
  411. void PrintAssigns( char *filename )
  412. {
  413.     BPTR handle;
  414.  
  415.     if (handle = HandlePrintStart(filename)) {
  416.         PrintFOneLine(handle, "\n  Address  Name             Path\n\n");
  417.         IterateList(PrintCallback, (void *)handle);
  418.     }
  419.  
  420.     HandlePrintStop();
  421. }
  422.  
  423. void SendAssList( void )
  424. {
  425.     IterateList(SendCallback, NULL);
  426. }
  427.  
  428.